package com;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Tindy
 * @date 2022/1/23
 * @describe
 */
@Slf4j
public class T {
    public static void main(String[] args) {
        String sql = "insert overwrite table kf_zt.account2 SELECT swjg_dm,swjgmc FROM (SELECT swjg_dm AS swjg_dm, swjgmc AS swjgmc FROM (SELECT swjg_dm,swjgmc FROM ( SELECT swjg_dm,swjgmc FROM yuangongs) table_350 UNION SELECT swjg_dm,swjgmc FROM (SELECT swjg_dm2,swjgmc2 FROM yuangong ) table_295) union_013) udf_882";
        String sql2="insert into table kf_zt.account2 values(2,3)";
        String sql3="delete from kf_zt.account2";
        String sql4="update kf_zt.account2 set name=2 where name=4";
        String sql5="select * from kf_zt.account2";
        String sql6="truncate kf_zt.account2,dfd";
        String sql7=sql+";"+sql2+";"+sql3+";"+sql4+";"+sql5+";"+sql6+";";

        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql6.toLowerCase(), JdbcConstants.HIVE);

        for (SQLStatement sqlStatement : sqlStatements) {
            System.out.println(((SQLTruncateStatement)sqlStatement).getTableSources().toString());
            SchemaStatVisitor schemaStatVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.HIVE);
            sqlStatement.accept(schemaStatVisitor);
            Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables();
            Collection<TableStat.Column> columns = schemaStatVisitor.getColumns();
            if (Objects.nonNull(tables)) {
                tables.forEach(((name, tableStat) -> {
                    if (tableStat.getCreateCount() > 0 || tableStat.getInsertCount() > 0) {
                        log.info("to: table[{}]", name.getName().toLowerCase());
                        columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {
                            log.info("to: table[{}] column[{}]", column.getTable().toLowerCase(), column.getName().toLowerCase());
                        });
                    } else  if (tableStat.getSelectCount() > 0) {
                        log.info("from: table[{}]", name.getName().toLowerCase());
                        columns.stream().filter(column -> Objects.equals(column.getTable().toLowerCase(), name.getName().toLowerCase())).forEach(column -> {
                            log.info("from: table[{}] column[{}]", column.getTable().toLowerCase(), column.getName().toLowerCase());
                        });
                    }
                }));
            }
        }
    }

}

